package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.dongdongshop.config.AlipayConfig;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.service.TbAddressService;
import com.dongdongshop.util.IdWorker;
import com.dongdongshop.vo.OderOderItemSend;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("my")
public class MyController {

    @Reference(timeout = 50000)
    private TbAddressService tbAddressService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Reference(timeout = 50000)
    private OrderService orderService;

    @RequestMapping("getOrderInfo")
    public String getOrderInfo(Model model){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        List<TbAddress> userAdr = tbAddressService.getOrderByUserId(user.getUsername());
        model.addAttribute("userAdr",userAdr);
        return "getOrderInfo";
    }

    @RequestMapping("addOrder")
    @ResponseBody
    public String addOrder(Long id,String WIDtotal_amount) throws AlipayApiException {
        //雪花id
        IdWorker idWorker = new IdWorker();
        long WIDout_trade_no = idWorker.nextId();
        String WIDsubject = "东东商品";
        String WIDbody ="东东描述";
        TbAddress tbAddress = tbAddressService.getAddress(id);
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        TbOrder tbOrder = new TbOrder();
        tbOrder.setUserId(user.getUsername());
        tbOrder.setReceiverAreaName(tbAddress.getAddress());
        tbOrder.setReceiverMobile(tbAddress.getMobile());
        tbOrder.setReceiver(tbAddress.getContact());
        tbOrder.setOrderNum(WIDout_trade_no +"");
        int i = orderService.addOrder(tbOrder);

        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        //同步回调路径
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        //异步回调路径
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + WIDout_trade_no + "\","
                + "\"total_amount\":\"" + WIDtotal_amount + "\","
                + "\"subject\":\"" + WIDsubject + "\","
                + "\"body\":\"" + WIDbody + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求支付宝,就是给我们构建一个from表单,我们拿到这个返回的form表单后直接输出到页面,然后页面就直接跳转到支付宝的页面
        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //输出 jsp 中out.println ==java中response.getWriter().write() == @responseBody
//        out.println(result);
        return result;
    }
    @RequestMapping("homeIndex")
    public String homeIndex(){
        return "home-index";
    }
    @RequestMapping("homeSetting")
    public String homeSetting(Model model){
        List<TbProvinces> provinceList = tbAddressService.selectAllProvince();
        model.addAttribute("list",provinceList);
        return "home-setting-address";
    }
    @RequestMapping("getAddrss")
    @ResponseBody
    public Result getAddrss(){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        List<TbAddress> addressList = tbAddressService.selectAddrssAll(user.getUsername());
        return Result.response(ResultEnum.SUCCESS).setData(addressList);
    }
    @RequestMapping("showCity")
    @ResponseBody
    public Result showCity(int id){
        String provinceid = tbAddressService.selectProvinceid(id);
        List<TbCities> citiesList = tbAddressService.showCity(provinceid);
        return Result.response(ResultEnum.SUCCESS).setData(citiesList);
    }
    @RequestMapping("showTwon")
    @ResponseBody
    public Result showTwon(int id){
        String cityid = tbAddressService.selectCityid(id);
        List<TbAreas> townList = tbAddressService.showTown(cityid);
        return Result.response(ResultEnum.SUCCESS).setData(townList);
    }
    @RequestMapping("addInfAdr")
    @ResponseBody
    public Result addInfAdr(int id, int id1, int id2, TbAddress tbAddress) throws IOException {
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        tbAddress.setUserId(user.getUsername());
        String provinceId = tbAddressService.selectProvince(id);
        tbAddress.setProvinceId(provinceId);
        String cityId = tbAddressService.selectCity(id1);
        tbAddress.setCityId(cityId);
        String areasId = tbAddressService.selectAreas(id2);
        tbAddress.setTownId(areasId);
        int i =tbAddressService.addInfAdr(tbAddress);
        return Result.response(ResultEnum.SUCCESS);
    }
    @RequestMapping("pay")
    public String pay (){
        return "pay";
    }
    @RequestMapping("trade")
    public String trade(){
        return "trade";
    }

    @RequestMapping("toSend")
    public String toSend(){
        return"home-order-send";
    }

    @RequestMapping("sendShow")
    @ResponseBody
    public Result sendShow(){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String status = "3";
        List<OderOderItemSend> send = orderService.sendShow(user,status);
        return Result.response(ResultEnum.SUCCESS).setData(send);
    }
    //待评价
    @RequestMapping("sendShow1")
    @ResponseBody
    public Result sendShow1(){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String status = "7";
        List<OderOderItemSend> send = orderService.sendShow(user,status);
        return Result.response(ResultEnum.SUCCESS).setData(send);
    }
    @RequestMapping("homeOrderEvaluate")
    public String homeOrderEvaluate(){
        return "home-order-evaluate";
    }

}
